<?php

!defined('IN_NOVA') && exit('Access Denied!');

abstract class Base {

	public function __construct() {
		// url 处理
		$len = strlen( APP_PATH ) - strlen( $_SERVER['REQUEST_URI'] );
		if( $len == 0 ) {
			$_GET['URL'] = explode( '/', '' );
		} else {
			$_GET['URL'] = explode( '/', substr( $_SERVER['REQUEST_URI'], $len ) );
		}

		// 一些安全处理
		if ( !get_magic_quotes_gpc() ) {
			$_GET = $this->add_slashes( $_GET );
			$_POST = $this->add_slashes( $_POST );
			$_COOKIE = $this->add_slashes( $_COOKIE );
		}
	}

    public function __set($name ,$value)
    {
        if(property_exists($this,$name)){
            $this->$name = $value;
        }
    }

    public function __get($name)
    {
        if(isset($this->$name)){
            return $this->$name;
        }else {
            return null;
        }
    }
   
    public function G( $var, $type = 'int' ) {
    	return $this->get( $var, $method = 'G', $type );
    }

    public function P( $var, $type = 'int' ) {
    	return $this->get( $var, $method = 'P', $type );
    }

    public function C( $var, $type = 'int' ) {
    	return $this->get( $var, $method = 'C', $type );
    }

    public function S( $var, $type = 'int' ) {
    	return $this->get( $var, $method = 'S', $type );
    }

	// 获取 $_GET、$_POST、$_COOKIE、$_SERVER值
	private function get( $var, $method = 'G', $type = 'string' ) {
		$temp = NULL;
		switch( $method ) {
			case 'G':
				$temp = isset($_GET[$var]) ? $_GET[$var] : NULL;
				break;
			case 'P':
				$temp = isset($_POST[$var]) ? $_POST[$var] : NULL;
				break;
			case 'C':
				$temp = isset($_COOKIE[$var]) ? $_COOKIE[$var] : NULL;
				break;
			case 'S':
				$temp = isset($_SERVER[$var]) ? $_SERVER[$var] : NULL;
				break;
		}
		switch( $type ) {
			case 'int':
				if( $temp == NULL ) {
					$temp = 0;
				}
				$temp = intval( $temp );
				break;
			case 'array':
				if( !is_array( $temp ) ) {
					$temp = array();;
				}
				break;
			case 'string':
				// 待添加
				$temp = $temp;
				break;
		}
		return $temp;
	}

	// URL 组装
	protected function U( $url, $module, $params = '', $extra = '' ) {
		if( !defined( 'APP_URLOP' ) || APP_URLOP != TRUE ) {
			$url =  $url . 'index.php?m=' . $module;
			if( $params != '' ) {
				$url =  $url . '&p=' . $params;
				if( is_array( $extra ) ) {
					foreach( $extra as $key => $value ) {
						$url = $url . '&' . $key . '=' . $value;
					}
				}
			}
		} else {
			$url =  $url . $module;
			if( $params != '' ) {
				$url =  $url . '/' . $params;
				if( is_array( $extra ) ) {
				   	foreach( $extra as $key => $value ) {
						$url = $url . '/' . $key . '_' . $value;
					}
				}
			}
			$url .= '.html';
		}
		return $url;
	}

	// URL 解析
	protected function _U() {
		if( !defined( 'APP_URLOP' ) || APP_URLOP != TRUE ) {
			return;
		}
		if( $this->G( 'm' ) != NULL ) {
			return;
		}
		$url = $this->G( 'URL', 'array' );
		switch( count( $url ) ) {
		case 1:
			if( $url[0] == '' ) {
				break;
			}
			$temp = substr ( $url[0], 0, strrpos ( $url[0], '.' ) );
			if( $temp == '' ) {
				$this->m = 'err_404';
				break;
			}
			$this->m = $temp;
			break;
		case 2:
			if( $url[0] == '' ) {
				$this->m = 'err_404';
				break;
			}
			$temp = substr ( $url[1], 0, strrpos ( $url[1], '.' ) );
			if( $temp == '' ) {
				$this->m = 'err_404';
				break;
			}
			$this->m = $url[0];
			$this->p = $temp;
			break;
		default:
			if( $url[0] == '' || $url[1] == '' ) {
				$this->m = 'err_404';
				break;
			}
			$this->m = $url[0];
			$this->p = $url[1];
			for( $i = 2 ; $i < count( $url ) ; $i ++ ) {
				if( $url[$i] == '' ) {
					$this->m = 'err_404';
					break;
				}
				if( $i == count( $url ) - 1 ) {
					$url[$i] = substr( $url[$i], 0, strrpos ( $url[$i], '.' ) );
				}
				$temp = explode( '_', $url[$i], 2 );
				if( count( $temp ) != 2 || $temp[0] == '' || $temp[1] == '' ) {
					$this->extra = $url[$i];
				} else {
					$this->$temp[0] = $temp[1];
				}
			}
			break;
		}
	}

}

?>
